import java.io.FileWriter;
import java.io.IOException;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

public class StandingsParser {
	
	static boolean name = false;
	static boolean conf = false;
	static boolean over = false;
	static boolean wins = false;
	static boolean losses = false;
	static boolean percent = false;
	static boolean print = false;
	static StandingsTeam team;
	static FileWriter writer;

	public static void main(String[] args) {
		
		try {
			writer = new FileWriter("C:\\Users\\Dallin\\Downloads\\2011-2012 Standings\\AEL-11WCC-STAND.csv");
			writer.append(" , Conf Wins, Conf Losses, Conf Win %, Overall Wins, Overall Losses, Overall Win %");
			writer.append('\n');
			 
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
		 
			DefaultHandler handler = new DefaultHandler() {
			 
				public void startElement(String uri, String localName,String qName, 
			                Attributes attributes) throws SAXException {
			 
					if (qName.equalsIgnoreCase("Team")) { name = true; }
					else if (qName.equalsIgnoreCase("Record")) {
						if (attributes.getValue("Type").equals("Overall"))
							over = true;
						else if (attributes.getValue("Type").equals("Conference"))
							conf = true;
						else
							System.out.println("Error!");						
					}
					else if (qName.equalsIgnoreCase("Wins")) { wins = true; }
					else if (qName.equalsIgnoreCase("Loss")) { losses = true; }
					else if (qName.equalsIgnoreCase("Win_Percentage")) { percent = true; }
					else if (qName.equalsIgnoreCase("heading") || qName.equalsIgnoreCase("category") || qName.equalsIgnoreCase("sport")) { print = true; }
			 
				}
			 
				public void endElement(String uri, String localName,
					String qName) throws SAXException {
			 
					if (qName.equalsIgnoreCase("Team")) { name = false; }
					else if (qName.equalsIgnoreCase("Wins")) { wins = false; }
					else if (qName.equalsIgnoreCase("Loss")) { losses = false; }
					else if (qName.equalsIgnoreCase("Win_Percentage")) { percent = false; }
					else if (qName.equalsIgnoreCase("Listing")) { System.out.println(team); try {
						writer.append(team.csv());
						writer.append('\n');
					} catch (IOException e) {
						e.printStackTrace();
					} }
			 
				}
			 
				public void characters(char ch[], int start, int length) throws SAXException {
			 
					if (name) {
						team = new StandingsTeam();
						String teamname = new String(ch, start, length);
						team.setName(teamname);
						name = false;
					}
					else if (conf && wins) {
						String wincount = new String(ch, start, length);
						team.setConfWins(Integer.parseInt(wincount));
					}
					else if (conf && losses) {
						String losscount = new String(ch, start, length);
						team.setConfLosses(Integer.parseInt(losscount));
					}
					else if (conf && percent) {
						String percentage = new String(ch, start, length);
						team.setConfPerc(Double.parseDouble(percentage));
						conf = false;
					}
					else if (over && wins) {
						String wincount = new String(ch, start, length);
						team.setOverWins(Integer.parseInt(wincount));
					}
					else if (over && losses) {
						String losscount = new String(ch, start, length);
						team.setOverLosses(Integer.parseInt(losscount));
					}
					else if (over && percent) {
						String percentage = new String(ch, start, length);
						team.setOverPerc(Double.parseDouble(percentage));
						over = false;
					}
					else if (print) {
						String toPrint = new String(ch, start, length);
						System.out.print(toPrint + " ");
						if(toPrint.equals("CBASK"))
							System.out.println("\n");
						print = false;
					}
			 
				}
		 
		    };
		 
		    saxParser.parse("C:\\Users\\Dallin\\Downloads\\2011-2012 Standings\\AEL-11WCC-STAND.xml", handler);
		    
		    writer.flush();
		    writer.close();
		    
		    } catch (Exception e) {
		    	e.printStackTrace();
		    }	
		
	}
}
